<?php
/**
* Django's templating engine (partial) PHP implementation
* @link http://djangoproject.com Django
* this file is from a webappkit package (kit)
* @link http://webappkit.net
* @package djtpl
* @author J.Ducastel <nospam0@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* generic template node class
* mostly static use, except for djtplTemplate root node
* @author J.Ducastel <nospam0@ducastel.name>
*/
class DjTplNode {

    /***
     * php4 constructor
     * @param DjTplTemplate $template owner document
     */
    function DjTplNode(&$template) {
        $this->template=&$template;
    }

    /**
    * builds itself from tokens array
    * @param djtplToken &$selfToken
    * @param array &$nextTokens djtplToken
    * @access public
    * @return bool
    */
    function buildFromTokens(&$selfToken, &$nextTokens) {
        //$this->token=&$selfToken;
        return true;
    }/**/

    /**
    * default rendering method
    * @param djtplContext &$context
    * @access public
    * @return string
    */
    function render(&$context) {

        if (!$this->isAtomic())
            return $this->renderChildNodes();

        return '';
    }/**/

    /**
    * is the node atomic (has no children) ?
    * @access public
    * @return bool
    */
    function isAtomic() {
        return true;
    }/**/

    /**
    * is a tag node ?
    * @access public
    * @return string|false tag name if relevant
    */
    function isTag($tag=null) {
        if (!is_a($this,'DjTplTagNode'))
            return false;
        // echo "<p>".get_class($this)." is tag $tag ?</p>";
        if ($tag)
            return $this->getTagName()==$tag;
        return true;
    }/**/

    /**
    * is a var node ?
    * @access public
    * @return bool
    */
    function isVar() {
        return is_a($this,'djtplVarNode');
    }/**/

    /**
    * is a text node ?
    * @access public
    * @return bool
    */
    function isText() {
        return is_a($this,'djtplTextNode');
    }/**/

    /**
    * render child nodes
    * @param djtplContext
    * @access protected
    * @return string
    */
    function renderChildNodes(&$context) {
        $rendered='';
        for ($n=0; $n<count($this->childnodes); $n++)
            $rendered.=$this->childnodes[$n]->render($context);
        return $rendered;
    }/**/

    /**
    * renders child nodes before a defined tag node
    * @param djtplContext &$context
    * @param string $tag tag name
    * @access protected
    * @return string
    */
    function renderChildNodesBeforeTag(&$context, $tag) {
        $rendered='';
        for ($n=0; $n<count($this->childnodes); $n++) {
            $node=&$this->childnodes[$n];
            if ($node->isTag() and $node->getTagName()==$tag)
                break;
            $rendered.=$node->render($context);
        }
        return $rendered;
    }/**/

    /**
    * renders child nodes after a defined tag node
    * @param djtplContext &$context
    * @param string $tag tag name
    * @access protected
    * @return string
    */
    function renderChildNodesAfterTag(&$context, $tag) {
        $rendered=''; $found=false;
        for ($n=0; $n<count($this->childnodes); $n++) {
            $node=&$this->childnodes[$n];
            if ($found)
                $rendered.=$node->render($context);
            if ($node->isTag() and $node->getTagName()==$tag)
                $found=true;
        }
        return $rendered;
    }

    /**
     * @var DjTplTemplate reference to parent template
     */
    var $template;
}
